# -*- coding:utf-8 -*-
import time
import common
import traceback
import GetEarthquakeData

# page = json.loads(requests.get('http://www.ceic.ac.cn/ajax/search?page=1&start=2001-10-24&end=2020-06-17&jingdu1=&&jingdu2=&weidu1=&weidu2=&height1=&height2=&zhenji1=&zhenji2=&_=1591863947475').text[1:-1])['num']
# for i in range(1, int(page) + 1):
#     GetChinaData('http://www.ceic.ac.cn/ajax/search?page={}&start=2001-10-24&end=2020-06-17&jingdu1=&&jingdu2=&weidu1=&weidu2=&height1=&height2=&zhenji1=&zhenji2=&_=1591863947475'.format(i)).save_to_database()
#     print('{:.2f%}'.format(i / int(page) * 100))
# GetChinaData('http://www.ceic.ac.cn/ajax/search?page=1&start=2001-10-24&end=2020-06-17&jingdu1=&&jingdu2=&weidu1=&weidu2=&height1=&height2=&zhenji1=&zhenji2=&_=1591863947475').save_to_database()
# GetUSAData('https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2020-06-14&endtime=2020-06-15&minmagnitude=4').save_to_database()
# GetEuropeData('https://www.emsc-csem.org/Earthquake/world/M4/?view=2').save_to_database()
# GetEarthquakeData.GetChinaDataRecent('http://news.ceic.ac.cn/index.html?time=1592304196').save_to_database()
# print(int(time.time() * 1000))
# sts = ''


# def main():
#     common.send_email('美国地质勘探局数据爬取开始！')
#     parameter = common.get_parameter()
#     intt = parameter['time']
#     while intt < time.time():
#         s = intt
#         t = time.localtime(intt)
#         start_time_str = '{}-{}-{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2))
#         global sts
#         sts = start_time_str
#         intt += 60 * 60 * 24
#         t = time.localtime(intt)
#         end_time_str = '{}-{}-{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2))
#         GetEarthquakeData.GetUSAData(
#             'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime={}&endtime={}&minmagnitude=4'.format(
#                 start_time_str, end_time_str)).save_to_database()
#         print("\r时间戳[{} -> {}]\t 时间[{} -> {}]".format(s, intt, start_time_str, end_time_str), end='')
#         parameter['time'] = intt
#         common.write_parameter(parameter)
#     common.send_email('美国地质勘探局数据爬取完成！')
#     print('\nfinish')

    # page = 1
    # while True:
    #     GetEuropeData('https://www.emsc-csem.org/Earthquake/world/M4/?view={}'.format(page)).save_to_database()
    #     page += 1
    #     print('page: {}'.format(page))


# if __name__ == '__main__':
#     while True:
#         try:
#             main()
#             break
#         except:
#             import traceback
#             common.send_email('当前进度：\n{}\n\n程序将在1分钟后继续\n\n异常信息：\n{}'.format(sts, traceback.format_exc()))
#             time.sleep(60)
#             continue


# t = time.localtime(time.mktime(time.strptime('2004-12-08 00:00:00', "%Y-%m-%d %H:%M:%S")))
# print(time.mktime(time.strptime('2004-12-07 00:00:00', "%Y-%m-%d %H:%M:%S")))
# time_str = '{}-{}-{} {}:{}:{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2), str(t[3]).zfill(2),
#                                       str(t[4]).zfill(2), str(t[5]).zfill(2))
# print(time_str)
# protext = ''


# def main():
#     common.send_email('欧洲地中海地震中心数据爬取开始！')
#     parameter = common.get_parameter()
#     page = parameter['page']
#     total_page = parameter['total_page']
#     while page > 0:
#         GetEarthquakeData.GetEuropeData('https://www.emsc-csem.org/Earthquake/world/M4/?view={}'.format(page)).save_to_database()
#         page -= 1
#         global protext
#         pro = (total_page - page) / total_page * 100
#         protext = "{:.2f}%\t{}/{}".format(pro, total_page - page, total_page)
#         print("\r当前进度{}".format(protext), end='')
#         parameter['page'] = page
#         common.write_parameter(parameter)
#     common.send_email('欧洲地中海地震中心数据爬取完成！')
#     print('\nfinish')



# if __name__ == '__main__':
#     while True:
#         try:
#             main()
#             break
#         except:
#             import traceback
#             common.send_email('当前进度：\n{}\n\n程序将在1分钟后继续\n\n异常信息：\n{}'.format(protext, traceback.format_exc()))
#             time.sleep(60)
#             continue


def main():
    try:
        url = 'http://news.ceic.ac.cn/index.html?time={}'.format(int(time.time()))
        GetEarthquakeData.GetChinaDataRecent(url).save_to_database()
    except:
        common.send_email('\n中国地震台网数据爬取异常\nURL：{}\n\n异常信息：{}'.format(url, traceback.format_exc()))

    t = time.localtime(time.time() - 3600 * 24)
    start_time_str = '{}-{}-{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2))
    t = time.localtime(time.time() + 3600 * 24)
    end_time_str = '{}-{}-{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2))
    try:
        url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime={}&endtime={}&minmagnitude=4'.format(start_time_str, end_time_str)
        GetEarthquakeData.GetUSAData(url).save_to_database()
    except:
        common.send_email('\n美国地质勘探中心数据爬取异常\nURL：{}\n\n异常信息：{}'.format(url, traceback.format_exc()))

    try:
        url = 'https://www.emsc-csem.org/Earthquake/world/M4/'
        GetEarthquakeData.GetEuropeData(url).save_to_database()
    except:
        common.send_email('\n欧洲地中海地震中心数据爬取异常\nURL：{}\n\n异常信息：{}'.format(url, traceback.format_exc()))


# if __name__ == '__main__':
#     main()

